import * as CANNON from "cannon-es";
import world from "../CANNON";
import WorldPlane from "../cannon/WorldPlane"
import controls from "../controls";
import Light from '../mesh/light'

export default class WorldCar {


  worldList = [];//地盘物体
  threeList = [];//轮子物体
  constructor(car) {
    // // 创建底盘
    const chassisShape = new CANNON.Box(new CANNON.Vec3(0.6, 0.6, 1.2))
    const chassisBody = new CANNON.Body({ mass: 100 })
    chassisBody.addShape(chassisShape)
    const vehicle = new CANNON.RigidVehicle({
      chassisBody
    })
    chassisBody.position.set(0, 3, 0)

    // chassisBody.position.set(3, 3, 3)
    // 创建初始的旋转四元数，使其与导入模型的旋转一致（假设需要旋转90°）
    // const initialRotation = new CANNON.Quaternion();
    // initialRotation.setFromEuler(Math.PI / 2, 0, 0); // 将旋转角度转换为弧度
    // chassisBody.quaternion.copy(initialRotation);
    // world.addBody(chassisBody)
    this.worldList.push(chassisBody)
    this.threeList.push(car[2])
    Light.spotLight.target = this.threeList[0]

    // this赋值底盘chassisBody
    const radius = 0.287
    const sphereShape1 = new CANNON.Sphere(radius)
    var initialRotation2 = new CANNON.Quaternion();
    // initialRotation2.setFromEuler(0, -Math.PI / 2, Math.PI / 2); // 绕Y轴旋转90°
    for (let i = 0; i < 4; i++) {
      const sphereBody1 = new CANNON.Body({ mass: 1, shape: sphereShape1 })
      // sphereBody1.position.set(car[3].position.x, car[3].position.y, car[3].position.z)

      sphereBody1.quaternion.set(initialRotation2.x, initialRotation2.y, initialRotation2.z, initialRotation2.w);
      vehicle.addWheel({
        axis: new CANNON.Vec3(1, 0, 0),
        body: sphereBody1,
        direction: new CANNON.Vec3(0, -1, 0),
        position: new CANNON.Vec3(car[i + 3].position.x, car[i + 3].position.y - 1.8, car[i + 3].position.z),
      })
      this.worldList.push(sphereBody1)
      this.threeList.push(car[i + 3])
    }



    world.addEventListener('postStep', () => {
      controls.target.set(this.worldList[0].position.x, this.worldList[0].position.y, this.worldList[0].position.z)
      Light.spotLight.position.set(this.worldList[0].position.x, Light.spotLight.position.y, this.worldList[0].position.z)
      // 更新 Three.js 球体位置
      this.threeList.forEach((item, index) => {
        item.position.copy(this.worldList[index].position);
        item.quaternion.copy(this.worldList[index].quaternion);
      })

    })

    document.addEventListener('keydown', (event) => {
      if (event.key === 'w') {
        vehicle.setWheelForce(50, 2)
        vehicle.setWheelForce(50, 3)
      }
      if (event.key === 's') {
        vehicle.setWheelForce(-50, 2)
        vehicle.setWheelForce(-50, 3)
      }

      if (event.key === 'd') {
        vehicle.setSteeringValue(-Math.PI / 4, 0)
        vehicle.setSteeringValue(-Math.PI / 4, 1)
      }
      if (event.key === 'a') {
        vehicle.setSteeringValue(Math.PI / 4, 0)
        vehicle.setSteeringValue(Math.PI / 4, 1)
      }

    })

    document.addEventListener('keyup', (event) => {
      if (event.key === 'w') {
        vehicle.setWheelForce(0, 2)
        vehicle.setWheelForce(0, 3)
      }
      if (event.key === 's') {
        vehicle.setWheelForce(0, 2)
        vehicle.setWheelForce(0, 3)
      }
      if (event.key === 'd') {
        vehicle.setSteeringValue(0, 0)
        vehicle.setSteeringValue(0, 1)
      }
      if (event.key === 'a') {
        vehicle.setSteeringValue(0, 0)
        vehicle.setSteeringValue(0, 1)
      }


    })



    vehicle.addToWorld(world)




  }

}

